package token

import (
	"context"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/api/internal/utils"
	"mall/service/backend/common/i18n"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetTokenByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetTokenByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTokenByIdLogic {
	return &GetTokenByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetTokenByIdLogic) GetTokenById(req *types.UUIDReq) (resp *types.SystemTokenInfoResp, err error) {
	data, err := l.svcCtx.BackendRpc.GetTokenById(l.ctx, &backendclient.UUIDReq{
		Id: req.Id,
	})
	if err != nil {
		return
	}

	option := utils.GetOptionByStatus(data.Status)

	return &types.SystemTokenInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: &types.SystemTokenInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        data.Id,
				CreatedAt: data.CreatedAt,
				UpdatedAt: data.UpdatedAt,
				CanAccept: option.CanAccept,
				CanReject: option.CanReject,
				CanRemove: option.CanRemove,
			},
			Uuid:      data.Uuid,
			Token:     data.Token,
			Source:    data.Source,
			Status:    data.Status,
			ExpiredAt: data.ExpiredAt,
		},
	}, nil
}
